#include "widget.h"
#include<QtGui>
#include<QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    for(int i = 0; i < 13;i++)
    {
        for(int j =0 ; j < 13; j++)
        {
            qizis[i][j] = 0;
        }
    }
    this->resize(800,800);
}
Widget::~Widget()
{
}
//
void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.save();
    QColor color1 = QColor(255,255,255);
    QColor color2 = QColor(0,0,0);
    QColor color3 = QColor(30,160,230);
    QColor color4 = QColor(200,200,200);
    painter.drawPixmap(100,100,600,600,QPixmap(":/beijingphoto/images/2.jpg"));
    painter.drawPixmap(100,0,200,100,QPixmap(":/beijingphoto/images/3.png"));
    //背景方框
    painter.setPen(color3);
    painter.setBrush(color4);
    for(int i =0; i < 13; i++)
    {
        painter.drawLine(startX+i * panWidth,startY, startX + i*panWidth, startY + 12*panWidth);
        painter.drawLine(startX,startY+i * panWidth, startX  + 12*panWidth, startY +i * panWidth);
    }
    //绘制棋子
    for(int i = 0; i < 13;i++)
    {
        for(int j =0 ; j < 13; j++)
        {
            QPoint center;
            center.setX( startX + i * panWidth);
            center.setY( startY + j * panWidth);
            if(qizis[i][j] == 1)
            {
                painter.setPen(color1);
                painter.setBrush(color1);
                painter.drawEllipse(center, rWidth, rWidth);
            }
            else if(qizis[i][j] == 2)
            {
                painter.setPen(color2);
                painter.setBrush(color2);
                painter.drawEllipse(center, rWidth, rWidth);
            }
        }
    }

    painter.restore();
}
//
void Widget::mousePressEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();
    for(int i = 0; i < 13;i++)
    {
        for(int j =0 ; j < 13; j++)
        {
            QRect rect = QRect(startX + i * panWidth - rWidth, startY + j * panWidth - rWidth, 2* rWidth, 2* rWidth);
            if(rect.contains(pos))
            {
                if(qizis[i][j] == 0)
                {
                    xiaQiZi(i,j);//下棋子
                }
                break;
            }
        }
    }
}
//下棋子
void Widget::xiaQiZi(int col, int row)
{
    int value = 0;
    if(isBlack)
        value = 2;
    else
        value = 1;
    isBlack = !isBlack;
    qizis[col][row] = value;
    this->update();
    QList<int> values;
    for(int i =0 ; i < 11; i++)
    {
        int x = col - 4 + i;
        int y = row;
        if(x >= 0 && x < 13)
        {
            values << qizis[x][y];
        }
    }
    if(isOk(values, value))
        return;
    values.clear();
    for(int i =0 ; i < 11; i++)
    {
        int x = col ;
        int y = row- 4 + i;
        if(x >= 0 && x < 13)
        {
            values << qizis[x][y];
        }
    }
    if(isOk(values, value))
        return;

    values.clear();
    for(int i =0 ; i < 11; i++)//将9改成11
    {
        int x = col -4 +i ;
        int y = row- 4 + i;
        if(x >= 0 && x < 13)
        {
            values << qizis[x][y];
        }
    }
    if(isOk(values, value))
        return;

    values.clear();
    for(int i =0 ; i < 11; i++)
    {
        int x = col + 4 - i ;
        int y = row- 4 + i;
        if(x >= 0 && x < 13)
        {
            values << qizis[x][y];
        }
    }
    if(isOk(values, value))
        return;
}
//是否赢了
bool Widget::isOk(QList<int> values,int value)
{
    bool start = false;
    int count = 0;
    for(int i =0 ; i < values.count();i++)
    {
        if(!start)
        {
            if(values[i] == value)
            {
                count=1;
                start = true;
            }
        }
        else
        {
            if(values[i] == value)
            {
                count++;
                if(count >=5)
                {
                    QMessageBox::information(this,"结束",QString(isBlack?QString("白棋子"):QString("黑棋子"))+"赢了！");
                    isBlack = true;
                    for(int i = 0; i < 13;i++)
                    {
                        for(int j =0 ; j < 13; j++)
                        {
                            qizis[i][j] = 0;
                        }
                    }
                    this->update();
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }
    return false;
}
